<template>
            <van-button
              class="follow-btn"
              type="info"
              color="#3296fa"
              round
              size="small"
              icon="plus"
              v-if="!isFollowed"
              @click="onFollow"
              :loading="followLoading"
              >关注</van-button
            >
            <van-button
                v-else
                class="follow-btn"
                round
                size="small"
                @click="onFollow"
                :loading="followLoading"
              >已关注</van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  // 告诉父组件你传的v-model我要改名称(接受的属性和发送事件名字)
  model: {
    prop: 'isFollowed',
    event: 'update-is-followed'
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },
  components: {},
  data () {
    return {
      followLoading: false
    }
  },
  created () {

  },
  methods: {
    async onFollow () {
      this.followLoading = true
      try {
        if (this.isFollowed) {
          await deleteFollow(this.userId)
        } else {
          await addFollow(this.userId)
        }
        this.$emit('update-is-followed', !this.isFollowed)
      } catch (e) {
        console.log(e)
        if (e.response && e.response.status === 400) {
          this.$toast('不能关注自己')
        } else {
          this.$toast('其他错误')
        }
      }
      this.followLoading = false
    }
  }
}
</script>

<style lang="less" scoped>

</style>
